package com.groupfive.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfive.exception.CustomException;
import com.groupfive.feign.client.DeptClient;
import com.groupfive.mapper.EmployeeMapper;
import com.groupfive.pojo.Account;
import com.groupfive.pojo.Dept;
import com.groupfive.pojo.Employee;
import com.groupfive.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DeptClient deptClient;

    /**
     * 查询所有
     */
    @Override
    public List<Employee> selectAll(Employee employee) {
        return employeeMapper.selectAll(employee);
    }

    /**
     * 根据id查询
     */
    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectById(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<Employee> selectPage(Employee employee, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        JSONObject jsonObject = JSONUtil.parseObj(deptClient.selectAll());
        Object data = jsonObject.get("data");
        List<Dept> deptList = JSONUtil.toList(data.toString(), Dept.class);

        List<Employee> list = employeeMapper.selectAll(employee);
        for (Employee e : list){
            Long deptId = e.getDeptId();
            if (deptId != null){
                for (Dept dept : deptList){
                    if (deptId.equals(dept.getId())){
                        e.setDeptName(dept.getName());
                    }
                }
            }
        }
        //List<Employee> list = employeeMapper.selectAll(employee);

        return PageInfo.of(list);
    }

    /**
     * 新增
     */
    @Override
    public int add(Employee employee) {
        String username = employee.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee != null){
            throw new CustomException("500","账号已存在");
        }
        if (employeeMapper.selectByNo(employee.getNo()) != null){
            throw new CustomException("500","请再次确认工号");
        }
        if (StrUtil.isBlank(employee.getPassword())){
            employee.setPassword("123");
        }
        if (StrUtil.isBlank(employee.getName())){
            employee.setName(employee.getUsername());
        }
        employee.setRole("EMP");
        return employeeMapper.insert(employee);
    }

    /**
     * 更新
     */
    @Override
    public int update(Employee employee) {
        return employeeMapper.update(employee);
    }

    /**
     * 删除
     */
    @Override
    public int deleteById(Long id) {
        return employeeMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            this.deleteById(id);
        }
    }

    @Override
    public Employee login(Account account) {
        String username = account.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee == null){
            throw new CustomException("500","账号不存在");
        }
        String password = account.getPassword();
        if (!password.equals(dbEmployee.getPassword())){
            throw new CustomException("500","账号或密码错误");
        }
        return dbEmployee;
    }

    @Override
    public void register(Employee employee) {
        this.add(employee);
    }

    @Override
    public void updatePassword(Account account) {
        Long id = account.getId();
        Employee employee = employeeMapper.selectById(id);
        if (!employee.getPassword().equals(account.getPassword())){
            throw new CustomException("500","原密码错误");
        }else if (employee.getPassword().equals(account.getNewPassword())){
            throw new CustomException("500","新密码与原密码相同");
        }
        employee.setPassword(account.getNewPassword());
        this.update(employee);
    }
}
